package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.OrderCountListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.OrderCollectContent;
import com.isunland.managesystem.entity.OrderCountQuery;
import com.isunland.managesystem.entity.OrderSaleCollectOriginal;
import com.isunland.managesystem.entity.OrderSaleCollectOriginal.OrderSaleCollectContent;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class OrderCountListFragment extends BaseListFragment {

  protected static final String EXTRA_VLAUE =
      "com.isunland.managesystem.ui.OrderCountListFragment.EXTRA_VLAUE";
  protected static final String EXTRA_QUERY =
      "com.isunland.managesystem.ui.OrderCountListFragment.EXTRA_QUERY";

  private static final int SEARCH_COUNT = 0;
  private ArrayList<OrderSaleCollectContent> mlist;
  private OrderCollectContent collectContent;
  private CurrentUser mUser;
  private String gropbystr =
      "APP_MAN_ID,APP_MAN_NAME,CUSTOMER_ID,CUSTOMER_NAME,MATERIAL_NAME,storage_name";
  private OrderCountQuery countQuery;//统计字段类对象

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.order_count_list);
    collectContent = (OrderCollectContent) getArguments().getSerializable(EXTRA_VLAUE);
    mUser = CurrentUser.newInstance(getActivity());
    countQuery =
        new OrderCountQuery("APP_MAN_ID,APP_MAN_NAME", "CUSTOMER_ID,CUSTOMER_NAME", "MATERIAL_NAME",
            "storage_name");
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

        break;
      //跳转统计查询界面
      case R.id.menu_item_query:

        Intent i = new Intent(getActivity(), SearchOrderCountActivity.class);
        i.putExtra(SearchOrderCountFragment.EXTRA_VLAUE, countQuery);
        i.putExtra(SearchOrderCountFragment.EXTRA_TYPE, collectContent.getStatisticType());
        startActivityForResult(i, SEARCH_COUNT);

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {

    return ApiConst.ULR_ORDER_COUNT;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", mUser.getMemberCode());
    params.put("statisticType", collectContent.getStatisticType());
    params.put("appManName", collectContent.getManName());
    params.put("ifBillAll", collectContent.getIfBillAll());
    params.put("customerId", collectContent.getCustomerId());
    params.put("beginappDate", collectContent.getStartDate());
    params.put("endappDate", collectContent.getEndDate());
    params.put("gropbystr", gropbystr);
    return params;
  }

  @Override public void resolveResponse(String json) {
    OrderSaleCollectOriginal original = new Gson().fromJson(json, OrderSaleCollectOriginal.class);
    ArrayList<OrderSaleCollectContent> rows = original.getRows();
    if (mlist == null) {
      mlist = new ArrayList<>();
    }
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist);
  }

  private void setAdapter(ArrayList<OrderSaleCollectContent> mlist2) {

    OrderCountListAdapter mApater =
        new OrderCountListAdapter(getActivity(), mlist2, collectContent.getStatisticType(),
            countQuery);
    setListAdapter(mApater);
    ((OrderCountListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    //查询
    if (requestCode == SEARCH_COUNT) {
      countQuery = (OrderCountQuery) data.getSerializableExtra(EXTRA_QUERY);
      gropbystr = getDate(countQuery);
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  private String getDate(OrderCountQuery countQuery2) {
    String str;
    StringBuilder buffer = new StringBuilder();
    if (!TextUtils.isEmpty(countQuery2.getAppMan())) {
      buffer.append(countQuery2.getAppMan()).append(",");
    }
    if (!TextUtils.isEmpty(countQuery2.getCustomer())) {
      buffer.append(countQuery2.getCustomer()).append(",");
    }
    if (!TextUtils.isEmpty(countQuery2.getMaterial())) {
      buffer.append(countQuery2.getMaterial()).append(",");
    }
    if (!TextUtils.isEmpty(countQuery2.getStorage())) {
      buffer.append(countQuery2.getStorage()).append(",");
    }
    str = buffer.toString();
    if (str.lastIndexOf(",") == str.length() - 1) {
      str = str.substring(0, str.length() - 1);
    }

    return str;
  }

  public static OrderCountListFragment newInstence(OrderCollectContent collectContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VLAUE, collectContent);
    OrderCountListFragment mfragment = new OrderCountListFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
